package com.lvwenguo.service.serviceImple;

import com.lvwenguo.dao.CategoryDao;
import com.lvwenguo.model.CategoryList;
import com.lvwenguo.service.CategoryListServiceInter;
import com.lvwenguo.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CategoryListServiceImple implements CategoryListServiceInter {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        private Connection connection;
        private CategoryDao categoryDao;
    private JdbcUtil jdbcUtil;
   public CategoryListServiceImple(){
        connection=JdbcUtil.getJdbcUtil().getConnertion();
        categoryDao=new CategoryDao(connection);
    }


    @Override
    public List<CategoryList> selectCategoryList() {
        String sql="select c.id,c.category_name from biz_category c where c.deleted=0";
        try {
            return categoryDao.selectList(sql,CategoryList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CategoryList> selectCategory(String name) {
        String sql="select c.id,c.category_name from biz_category c where c.category_name like ?";
        Object[] objects={"%"+name+"%"};
        try {
            return categoryDao.selectList(sql,objects,CategoryList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int AddCategory(String name) {
        String sql = "select c.id,c.category_name from biz_category c where c.category_name=?";
        Object[] objects={name};
        try {
            CategoryList categoryList = categoryDao.selectOnt(sql, objects, CategoryList.class);
            //表示有重复信息
            if(categoryList!=null || categoryList.getId()!=0){
                return 0;
            }
            else{
                String sqladd="INSERT into biz_category(category_name,create_time,update_time,deleted) values(?,?,?,?)";
                Object[] objects1={name,sdf.parse(sdf.format(new Date())),sdf.parse(sdf.format(new Date())),0};
                return categoryDao.Add(sql,objects1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    //删除品类信息
    public int deleteCategory(int id) {
        String sql = "UPDATE biz_category c set c.deleted=1 where c.id=?";
        Object[] objects = {id};
        try {
            return categoryDao.update(sql, objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
